package com.tboss.template.system.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.tboss.template.system.dto.GroupDto;
import com.tboss.template.system.dto.MapGrpFnDto;
import com.tboss.template.system.service.GroupService;

@Controller
@RequestMapping(value = "/template/system/group")
public class GroupController {
	
	@Autowired private GroupService grpService;
	
	@RequestMapping(value = {"/_list{reqPage}", "/_list/{reqPage}", "/_listConts/{reqPage}"}, 
			method = {RequestMethod.POST, RequestMethod.GET})
	
	public String grpList(@PathVariable("reqPage") Integer reqPage, @ModelAttribute GroupDto dto, 
			Model model, HttpServletRequest req){
		
		String rtnUrl = req.getServletPath().substring(1);
		if(reqPage != null) rtnUrl = rtnUrl.substring(0, rtnUrl.lastIndexOf("/"));
		
		
		this.grpService.setModelGrpList(reqPage, model, dto);

		return rtnUrl+"Ajax";
	}
	
	@RequestMapping(value = {"/_regForm"}, method = {RequestMethod.POST, RequestMethod.GET})//
	public String grpRegForm(Model model, HttpServletRequest req){
		String rtnUrl = req.getServletPath().substring(1);
		
		this.grpService.grpRegForm(model);
		
		return rtnUrl+"Ajax";
	}

	@RequestMapping(value = {"/_regFormSave"}, method = {RequestMethod.POST})//
	public Model grpRegFormSave(@ModelAttribute GroupDto dto, Model model, HttpServletRequest req){
		this.grpService.addGrpInfo(dto);

		return model;
	}
	
	@RequestMapping(value = {"/_regFormRevise/{grpId}"}, method = {RequestMethod.POST, RequestMethod.GET})//
	public String grpRegFormRevise(Model model, @PathVariable("grpId") String grpId, HttpServletRequest req){
		String rtnUrl = req.getServletPath().substring(1);
		if(grpId != null) rtnUrl = rtnUrl.substring(0, rtnUrl.lastIndexOf("Revise/"+grpId));
		
		this.grpService.grpRegForm(model, grpId);
		
		return rtnUrl+"Ajax";
	}
	
	@RequestMapping(value = {"/_regFormReviseSave"}, method = {RequestMethod.POST})//
	public Model grpRegFormReviseSave(@ModelAttribute GroupDto dto, Model model, HttpServletRequest req){
		this.grpService.updateGrpInfo(dto);
		
		return model;
	}
	
	@RequestMapping(value = {"/_detail/{grpId}"}, method = {RequestMethod.POST, RequestMethod.GET})//
	public String grpInfoDetail(Model model, @PathVariable("grpId") String grpId, HttpServletRequest req){
		String rtnUrl = req.getServletPath().substring(1);
		if(grpId != null) rtnUrl = rtnUrl.substring(0, rtnUrl.lastIndexOf("/"+grpId));
		
		this.grpService.grpRegForm(model, grpId);
		
		return rtnUrl+"Ajax";
	}
	
	@RequestMapping(value = {"/_members/{grpId}"}, method = {RequestMethod.POST, RequestMethod.GET})//
	public String grpMembers(Model model, @PathVariable("grpId") String grpId, HttpServletRequest req){
		String rtnUrl = req.getServletPath().substring(1);
		if(grpId != null) rtnUrl = rtnUrl.substring(0, rtnUrl.lastIndexOf("/"+grpId));
		
		this.grpService.grpMembers(model, grpId);
		
		return rtnUrl+"Ajax";
	}
	
	@RequestMapping(value = {"/_fnMgrForm/{grpId}"}, method = {RequestMethod.POST})
	public String groupFnMgrForm(@PathVariable("grpId") String grpId, @ModelAttribute GroupDto dto, 
			Model model, HttpServletRequest req){
		
		String rtnUrl = req.getServletPath().substring(1);
		if(grpId != null) rtnUrl = rtnUrl.substring(0, rtnUrl.lastIndexOf("/"+grpId));
		
		this.grpService.groupFnMgrForm(model, grpId, dto);
		
		return rtnUrl+"Ajax";
	}
	
	@RequestMapping(value = {"/_fnMgrFormSave/{grpId}"}, method = {RequestMethod.POST})
	public Model groupFnMgrFormSave(@ModelAttribute("items") MapGrpFnDto items, @PathVariable("grpId") String grpId
			, Model model, HttpServletRequest req){
		
		this.grpService.groupFnMgrFormSave(grpId, items);
	
		return model;
	}
	
	@RequestMapping(value = {"/_fnMgrFormView/{fnId}"}, method = {RequestMethod.POST})
	public String groupFnMgrFormView(@PathVariable("fnId") String fnId, @ModelAttribute GroupDto dto
			, Model model, HttpServletRequest req){
		
		String rtnUrl = req.getServletPath().substring(1);
		if(fnId != null) rtnUrl = rtnUrl.substring(0, rtnUrl.lastIndexOf("/"+fnId));
		
		this.grpService.groupFnMgrFormView(model, fnId, dto);
		
		return rtnUrl+"Ajax";
	}
	
}
